layui.use(['element', 'jquery', 'layer', 'laytpl','laypage','form','table'], function() {
    var $ = layui.jquery,
        layer = layui.layer,
        laytpl = layui.laytpl,
        laypage=layui.laypage,
        form=layui.form,
        element = layui.element
    table = layui.table;

    $(document).ready(function () {
        var user1_tb = table.render({
            elem: '#user1_tb'
            ,url:'/user/list1'
            ,cols: [[
                {field:'userId', title:'用户ID', fixed: 'left',  sort: true}
                ,{field: 'userName', title: '用户名',}
                ,{field: 'email',
                    title: '邮箱',
                    templet: function (res) {
                        return '<em>' + res.email + '</em>';
                    }}
                ,{field: 'joinTime', title: '注册时间',}
                ,{fixed: 'right', title:'操作', toolbar: '#user1_tb_bar', width:350}
            ]]
            /*page: true,
            height: 500*/
            /*,page: 2
            ,limit:10
            ,limits:[5,15,30,60,100]*/
        });
    });



    //监听行工具事件
    table.on('tool(user1_tb)', function(obj){
        var data = obj.data;
        console.log(data);
        if(obj.event === 'del'){
            layer.confirm('真的注销个人信息么', function(index){
                $.ajax({
                    url: '/user/list/'+data.userId,
                    type: 'delete',
                    dataType: 'json',
                    success: function(res) {
                        if (res.code != 0) {
                            return layer.msg("删除失败：" + res.msg, {
                                icon: 2
                            });
                        }
                        return layer.msg("删除成功", {
                            icon: 1,
                            time: 1300
                        }, function() {
                            obj.del();
                            //跳转到登录页面
                            window.location.href = '/logout';
                        });

                    }
                });
                layer.close(index);
            });
        } else if(obj.event === 'update'){
            layer.open({
                type: 1,
                title: '编辑个人信息',
                content: $("#user_form_tmpl").html(),
                area: ['400px'],
                btn: ['更新'],
                yes: function(index1) {
                    let new_data=form.val("user-form");
                    console.log(new_data);
                    // 校验两次密码是否一致
                  /*  form.verify({
                        confirmPass:function(value){
                            if($('input[name=password]').val() !== value)
                                return '两次密码输入不一致！';
                        }
                    });*/
                    if(new_data.password!=new_data.password1){
                        return layer.msg("两次密码输入不一致！：" , {
                            icon: 2
                        });
                    }else
                        $.ajax({
                        url: '/user/update',
                        type: 'post',
                        data: JSON.stringify(new_data),
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function(res) {
                            if (res.code != 0) {
                                return layer.msg(res.msg, {
                                    icon: 2
                                });
                            }
                            return layer.msg("更新成功", {
                                icon: 1,
                                time: 1300
                            }, function() {
                                obj.update(new_data);
                                layer.close(index1);
                            });
                        }
                    });
                },
                success: function() {
                    //填充表单（编辑状态）
                    form.val("user-form", data);
                }
            });

        }
    });


});
